<?php
namespace app\api\controller;

use app\model\company\CompanyUser;
use app\model\MyException;
use app\model\OperateLog;
use app\model\Tool;
use app\登录用户名;
use think\App;
use function think\p;

/**
 * 带验证token基础类
 * Class VerifyController
 * @package app
 */
class  ApiController
{
    /**
     * 应用实例
     */
    protected $app;
    protected $request;


    /**
     * 存放系统时间-很多地方使用，现在定义了后面直接用
     * @var false|string
     */
    public $time;

    /**
     * 用来临时存放返回数据里的data
     * @var
     */
    private $data="";
    private $code=200;
    private $message="";



    public $post;
    public $get;

    /**
     * @var array
     * 我来定义前置操作，所谓前置操作就是在我配置的操作执行之前执行我
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
        $this->time = date("Y-m-d H:i:s", time());
        $this->setGet();
        $this->setPost();
    }

    // 防注入，get和post参数做处理
    public function setGet()
    {
        if($this->app->request->isGet()){
            $param1 = json_decode(file_get_contents('php://input'), true);
            $param1 = empty($param1) ? array() : $param1;
            $param2 = $this->request->get();
            foreach ($param1 as $key=>$val){
                $this->get[$key] = addslashes($val);
            }
            foreach ($param2 as $key=>$val){
                $this->get[$key] = addslashes($val);
            }
        }
        unset($this->post['token']);
    }

    // 防注入，get和post参数做处理
    public function setPost()
    {
        if($this->app->request->isPost()){
            $param1 = json_decode(file_get_contents('php://input'), true);
            $param1 = empty($param1) ? array() : $param1;
            $param2 = $this->request->post();
            foreach ($param1 as $key=>$val){
                if(!is_array($val)){
                    $this->post[$key] = addslashes($val);
                }else{
                    $this->post[$key] = $val;
                }
            }
            foreach ($param2 as $key=>$val){
                if(!is_array($val)){
                    $this->post[$key] = addslashes($val);
                }else{
                    $this->post[$key] = $val;
                }
            }
        }
        unset($this->post['token']);
        // 过滤addslashes()
//        foreach ($this->app->request->post() as $key => $val){
//            if($key != "token"){
//                $this->post[$key] = addslashes($val);
//            }
//        }
    }

    public function setCode($code)
    {
        $this->code = $code;
    }

    public function getCode()
    {
        return $this->code;
    }

    public function setData($data){
        $this->data = $data;
    }

    public function getData(){
        return $this->data;
    }

    public function setMessage($message)
    {
        $this->message = $message;
    }

    public function sendJson()
    {
        header('Content-type:application/json; charset=utf-8');
        echo json_encode(
            array(
                'code' => $this->code,
                'message' => $this->message,
                'data' => $this->data,
            ),JSON_UNESCAPED_UNICODE
        );
        exit();
    }
}